package flown.window.graphics.ui;

import flown.window.graphics.Renderer2D;

public class Label extends Widget
{
    public String text = "";
    private float font_size = 0;

    public Label(String text)
    {
        this(text, 0, 0, 0, 1);
    }
    public Label(String text, int x, int y)
    {
        this(text, x, y, 0, 0);
    }
    public Label(String text, int x, int y, int width, int height)
    {
        super(x, y, width, height);
        this.text = text;
        this.FontSizeUpdate();
    }

    private void FontSizeUpdate()
    {
        //宽400，高120，字体大小80
        //400/120 = 40/12 = 20/6 = 10/3
        //10/3 * ratio = 80
        //font_size = 宽/高 * ratio
        //ratio = 24
        this.font_size = (float)(this.rect.width / this.rect.height * 24);
    }

    @Override public final void SetBounds(int x, int y, int width, int height)
    {
        int original_width = this.rect.width;
        int original_height = this.rect.height;
        super.SetBounds(x, y, width, height);

        if (this.rect.width != original_width || this.rect.height != original_height)
            this.FontSizeUpdate();
    }
    @Override public final void Render(Renderer2D renderer)
    {
        if (renderer.GetGraphics2D().getFont().getSize() != this.font_size)
            renderer.SetFontSize(this.font_size);
        renderer.DrawText(this.text, this.rect.x, this.rect.y);
    }


    public final void SetText(String text) {this.text = text;}
    public final String GetText() {return this.text;}
}
